package com.example.arithmeticleetcode.leetcode.nov;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-12-02 09:08
 **/
public class Demo24 {

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(0);
        listNode1.next = new ListNode(1);
        listNode1.next.next = new ListNode(2);
        listNode1.next.next.next = new ListNode(3);
        listNode1.next.next.next.next = new ListNode(4);
        listNode1.next.next.next.next.next = new ListNode(5);

        ListNode listNode2= new ListNode(1000000);
        listNode2.next = new ListNode(1000001);
        listNode2.next.next = new ListNode(1000002);
        System.out.println(mergeInBetween(listNode1, 3, 4, listNode2));


    }

    public static ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode listNode = new ListNode(0);
        ListNode current = listNode;
        ListNode head1 = list1;
        ListNode head2 = list2;

        while (head1 != null) {
            if (head1.next != null && head1.next.val == b) {
                while (head2 != null && head2.next != null) {
                    head2 = head2.next;
                }
                head2.next = head1.next.next;
            }
            head1 = head1.next;
        }

        while (list1 != null) {
            current.next = list1;
            current = list1;
            if (list1.next != null && list1.next.val == a) {
                current.next = list2;
                break;
            }
            list1 = list1.next;
        }
        return listNode.next;
    }
}
